﻿Imports System.IO
Imports System.Runtime.Serialization

Imports IntoNails.Models

<Serializable()> _
Public Class UserRepository
    Inherits AbstractFileRepository(Of User)
    Implements IUserRepository, ISerializable

#Region "Constructors"

    Public Sub New(ByVal dataFile As String)
        Me.DataFile = dataFile
        Me.Load()
    End Sub

    Public Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
        Me.InnerDataList = DirectCast(info.GetValue("EmployeeList", GetType(List(Of User))), List(Of User))
    End Sub

    Public Sub GetObjectData(ByVal info As SerializationInfo, ByVal ctxt As StreamingContext) _
        Implements ISerializable.GetObjectData
        info.AddValue("UserList", Me.InnerDataList)
    End Sub

#End Region

    Public Function Add(ByVal data As User) As Boolean _
        Implements IDataRepository(Of User).Add

        If data Is Nothing Then
            Return False
        End If

        If Not Me.InnerDataList.Contains(data) Then
            Me.InnerDataList.Add(data)
            Return True
        End If

        Return False
    End Function

    Public Function GetItem(ByVal uniqueCode As Integer) As User _
        Implements IDataRepository(Of User).GetItem

        Return Me.InnerDataList.Where(Function(u) u.EmployeeCode = uniqueCode).FirstOrDefault()

    End Function

    Public Function GetRawCollection() As List(Of User) _
        Implements IDataRepository(Of User).GetRawCollection

        Return Me.InnerDataList

    End Function

    Public Function Remove(ByVal uniqueCode As Integer) As Boolean _
        Implements IDataRepository(Of User).Remove

        ' TODO: find a way to block user by employee-code...
        Return False

    End Function

    Public Function Update(ByVal uniqueCode As Integer, ByVal newData As User) As Boolean _
        Implements IDataRepository(Of User).Update

        ' TODO: find a way to update user by employee-code...
        Return False

    End Function

    Public Function Login(ByVal username As String, ByVal password As String) As User _
        Implements IUserRepository.Login

        Return Me.InnerDataList.Where(Function(u) u.Username = username And u.Password = password.Hash()).FirstOrDefault()

    End Function

    Public Function GetUser(ByVal username As String) As User _
        Implements IUserRepository.GetUser

        Return Me.InnerDataList.Where(Function(u) u.Username = username).FirstOrDefault()

    End Function

#Region "RepositoryBase Overriden"

    Public Overrides Sub Load()
        If IO.File.Exists(Me.DataFile) Then
            Using readStream As New FileStream(Me.DataFile, FileMode.Open, FileAccess.Read)
                Me.InnerDataList = readStream.Deserialize(Of List(Of User))()
            End Using
        Else
            Me.InnerDataList = New List(Of User)()
        End If
    End Sub

    Public Overrides Sub Save()
        Using writeStream As New FileStream(Me.DataFile, FileMode.OpenOrCreate, FileAccess.Write)
            Me.InnerDataList.Serialize(writeStream)
        End Using
    End Sub

#End Region

End Class